package task4

import akka.actor.{Actor, ActorSystem, Props}

class AActor extends Actor{
  override def receive: Receive = {
    case i: Int => {
      if(i <= 10) {
        println(s"AActor: 厉害，看我佛山无影脚。。。第$i 脚")
        sender ! i + 1
      } else{
        context.stop(self)
        context.system.terminate()
      }
    }
    case str: String => {
      val bActor = context.actorSelection("akka://simpleFactory/user/BActor")
      println(str)
      println("start, ok!")
      println("我打")
      bActor ! 1
    }
  }
}

class BActor extends  Actor{
  override def receive: Receive = {
    case i: Int =>{
      if(i < 10){
        println(s"BActor: 挺猛，看我降龙十八掌。。。第$i 掌")
        sender ! i
      } else{
        context.stop(self)
        context.system.terminate()
      }
    }
  }
}

object ActorDemo {
  def main(args: Array[String]): Unit = {
    val simpleFactory = ActorSystem("simpleFactory")
    val aActor = simpleFactory.actorOf(Props[AActor], "AActor")
    val bActor = simpleFactory.actorOf(Props[BActor], "BActor")

    aActor ! "AActor 出招了"
  }
}
